﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SqlServer.Types;
using System.Collections.ObjectModel;
using GeoVision.Addin.Core.Spatial;
using GeoVision.Addin.Core.ShapeCore;

namespace GeoVision.Addin.Helpers
{
    public static class GeometryHelper
    {
        public static GeometryType GetGeometryType(string val)
        {
            switch (val.ToUpperInvariant())
            {
                case "POINT": return GeometryType.Point;
                case "LINESTRING": return GeometryType.LineString;
                case "POLYGON": return GeometryType.Polygon;
                case "GEOMETRYCOLLECTION": return GeometryType.GeometryCollection;
                case "MULTIPOINT": return GeometryType.MultiPoint;
                case "MULTILINESTRING": return GeometryType.MultiLineString;
                case "MULTIPOLYGON": return GeometryType.MultiPolygon;
                default: return GeometryType.Unknown;
            }
        }

        public static SqlGeometry[] Points(this SqlGeometry geometry)
        {
            List<SqlGeometry> points = new List<SqlGeometry>();
            for (int i = 1; i <= geometry.STNumPoints(); i++)
            {
                points.Add(geometry.STPointN(i));
            }
            return points.ToArray();
        }
        public static Array ToPointFArray(this SqlGeometry[] geometryArray,double _divisionPercent)
        {
            List<double> list = new List<double>();
            foreach (var item in geometryArray)
            {
                list.Add((double)item.STX / _divisionPercent);
                list.Add((double)item.STY / _divisionPercent);
            }
            return list.ToArray();
        }
        public static Array ConvertArrayFromPoints(Collection<System.Windows.Point> points, double _divisionPercent)
        {
            List<double> list = new List<double>();
            foreach (var item in points)
            {
                list.Add((double)item.X /_divisionPercent);
                list.Add((double)item.Y / _divisionPercent);
            }
            return list.ToArray();
        }
    }
}
